# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 7: Additional subgroup comparisons (Appendix)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(cjoint)
library(ggthemes)

# Load relevant functions -------------------------------------------------

source("functions/specify_parameters.R")
source("functions/estimate_AMCEs.R")
source("functions/compare_AMCEs.R")
source("functions/order_attributes_levels.R")
source("functions/set_my_ggtheme.R")
source("functions/plot_AMCEs.R")
source("functions/specify_color_palette.R")
source("functions/plot_AMCEs_comparison.R")

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_donors.RData")
load("temp/data_for_analysis_public.RData")

# Specify parameters ------------------------------------------------------

parameters <- list(list(), list(), list())

w_race <- c(0.6, 0.2, 0.2)

# Selection of baselines

parameters <- specify_parameters(df_cleaned_donors, "Gender", "Male", 1)
parameters <- specify_parameters(df_cleaned_donors, "Race", "White", 0, w_race)
parameters <- specify_parameters(df_cleaned_donors, "Partisanship", "Republican", 1)
parameters <- specify_parameters(df_cleaned_donors, "Taxes", "Less progressive", 1) 
parameters <- specify_parameters(df_cleaned_donors, "Discrimination", "Not a big problem", 1)
parameters <- specify_parameters(df_cleaned_donors, "Courts", "Obey courts", 1) 
parameters <- specify_parameters(df_cleaned_donors, "Investigations", "Independent", 1)
parameters <- specify_parameters(df_cleaned_donors, "Compromise", "Supports compromise", 1) 
parameters <- specify_parameters(df_cleaned_donors, "Voting", "Opposes voter ID laws", 1)

constraint_list <- list()

# Create model 

model <- formula(selected ~ 
                   `Gender` +
                   `Race` +
                   `Partisanship` +
                   `Taxes` +
                   `Discrimination` +
                   `Courts` +
                   `Investigations` + 
                   `Compromise` +
                   `Voting`)

# Set the order of attributes and levels ----------------------------------

Attribute_order <- c("Gender",
                     "Race",
                     "Partisanship",
                     "Taxes",
                     "Discrimination",
                     "Courts",
                     "Investigations",
                     "Compromise",
                     "Voting")

Level_order <- c(c("Male",
                   "Female"),
                 c("White",
                   "Black",
                   "Hispanic"),
                 c("Republican",
                   "Democrat"),
                 c("Less progressive",
                   "More progressive"),
                 c("Not a big problem",
                   "Prevent discrimination"),
                 c("Obey courts",
                   "Disregard politicized decisions"),
                 c("Independent", 
                   "Partisan involvement"),
                 c("Supports compromise", 
                   "Stand up to other party"),
                 c("Opposes voter ID laws", 
                   "Supports voter ID laws"))


# Estimate AMCEs ----------------------------------------------------------

df <- df_cleaned_public

# Set color palette

default_colour_palette <- estimate_AMCEs("AMCEs (Public, March 2019)", df, model, weights = TRUE) %>% 
  specify_color_palette(length(Attribute_order))

# Main results using all respondents (weighted vs. unweighted)

all.weighted.public <- estimate_AMCEs("AMCEs (Public, March 2019)", df, model, weights = TRUE)
all.unweighted.public <- estimate_AMCEs("AMCE (Public, unweighted, March 2019)", df, model, weights = FALSE)

comparison1 <- compare_AMCEs(all.weighted.public, all.unweighted.public, "All (weighted)", "All (unweighted)")
plot_AMCEs_comparison("Comparing Weighted vs. Unweighted Public Sample", comparison1)

# Subgroup analyses -------------------------------------------------------

# Republican vs. Democrat 

republican.public <- df %>% filter(partisanship_dum_7 == 1) 
democrat.public <- df %>% filter(partisanship_dum_7 == 2) 

republican.public <- estimate_AMCEs("AMCEs, Republican (Public, March 2019)", republican.public, model, weights = TRUE)
democrat.public <- estimate_AMCEs("AMCEs, Democrat (Public, March 2019)", democrat.public, model, weights = TRUE)

comparison2 <- compare_AMCEs(republican.public, democrat.public, "Republicans", "Democrats")
plot_AMCEs_comparison("Comparing Republicans vs. Democrats (Public, March 2019)", comparison2)

# Trump approvers vs. disapprovers

pro.trump <- df %>% filter(trump_dum == 1)
anti.trump <- df %>% filter(trump_dum == 2)

pro.trump <- estimate_AMCEs("AMCEs, Pro-Trump (Public, March 2019)", pro.trump, model, weights = TRUE)
anti.trump <- estimate_AMCEs("AMCEs, Anti-Trump (Public, March 2019)", anti.trump, model, weights = TRUE)

comparison3 <- compare_AMCEs(pro.trump, anti.trump, "Trump approvers", "Trump disapprovers")
plot_AMCEs_comparison("Comparing Trump Approvers vs. Trump Disapprovers (Public, March 2019)", comparison3)

# High vs. low political knowledge

high.pk <- df %>% filter(knowledge_dum == 2) 
low.pk <- df %>% filter(knowledge_dum == 1) 

high.pk <- estimate_AMCEs("AMCEs, High political knowledge (Public, March 2019)", high.pk, model, weights = TRUE)
low.pk <- estimate_AMCEs("AMCEs, Low political knowledge (Public, March 2019)", low.pk, model, weights = TRUE)

comparison4 <- compare_AMCEs(high.pk, low.pk, "High political knowledge", "Low political knowledge")
plot_AMCEs_comparison("Comparing High vs. Low Political Knowledge (Public, March 2019)", comparison4)

# High vs. low education level

high.educ <- df %>% filter(education_dum == 2) 
low.educ <- df %>% filter(education_dum == 1) 

high.educ <- estimate_AMCEs("AMCEs, Higher education level (Public, March 2019)", high.educ, model, weights = TRUE)
low.educ <- estimate_AMCEs("AMCEs, Lower education level (Public, March 2019)", low.educ, model, weights = TRUE)

comparison5 <- compare_AMCEs(high.educ, low.educ, "Bachelor's degree or higher", "Lower than bachelor's degree")
plot_AMCEs_comparison("Comparing Higher vs. Lower Education Level (Public, March 2019)", comparison5)

# High vs. low political interest

high.int <- df %>% filter(interest_dum == 2) 
low.int <- df %>% filter(interest_dum == 1) 

high.int <- estimate_AMCEs("AMCEs, Higher political interest (Public, March 2019)", high.int, model, weights = TRUE)
low.int <- estimate_AMCEs("AMCEs, Lower political interest (Public, March 2019)", low.int, model, weights = TRUE)

comparison6 <- compare_AMCEs(high.int, low.int, "High political interest", "Low political interest")
plot_AMCEs_comparison("Comparing High vs. Low Political Interest (Public, March 2019)", comparison6)

# Older vs. younger

older <- df %>% filter(age_dum == 2) 
younger <- df %>% filter(age_dum == 1) 

older <- estimate_AMCEs("AMCEs, Older (Public, March 2019)", older, model, weights = TRUE)
younger <- estimate_AMCEs("AMCEs, Younger (Public, March 2019)", younger, model, weights = TRUE)

comparison7 <- compare_AMCEs(older, younger, "Above median", "Below median")
plot_AMCEs_comparison("Comparing Older vs. Younger (Public, March 2019)", comparison7)

# Trump-approving Democrats vs. Trump-disapproving Democrats 

protrump.dem <- df %>% filter(partisanship_dum_7 == 2 & trump_dum == 1)
antitrump.dem <- df %>% filter(partisanship_dum_7 == 2 & trump_dum == 2)

protrump.dem <- estimate_AMCEs("AMCEs, Pro-Trump Democrats (Public, March 2019)", protrump.dem, model, weights = TRUE)
antitrump.dem <- estimate_AMCEs("AMCEs, Anti-Trump Democrats (Public, March 2019)", antitrump.dem, model, weights = TRUE)

comparison8 <- compare_AMCEs(protrump.dem, antitrump.dem, "Pro-Trump Democrats", "Anti-Trump Democrats")
plot_AMCEs_comparison("Comparing Pro- vs. Anti-Trump Democrats (Public, March 2019)", comparison8)

# Trump-approving Republicans vs. Trump-disapproving Republicans

protrump.rep <- df %>% filter(partisanship_dum_7 == 1 & trump_dum == 1)
antitrump.rep <- df %>% filter(partisanship_dum_7 == 1 & trump_dum == 2)

protrump.rep <- estimate_AMCEs("AMCEs, Pro-Trump Republicans (Public, March 2019)", protrump.rep, model, weights = TRUE)
antitrump.rep <- estimate_AMCEs("AMCEs, Anti-Trump Republicans (Public, March 2019)", antitrump.rep, model, weights = TRUE)

comparison9 <- compare_AMCEs(protrump.rep, antitrump.rep, "Pro-Trump Republicans", "Anti-Trump Republicans")
plot_AMCEs_comparison("Comparing Pro- vs. Anti-Trump Republicans (Public, March 2019)", comparison9)

# Public vs. donors

all.donors <- estimate_AMCEs("AMCEs (Donors, March 2019)", df_cleaned_donors, model)
comparison10 <- compare_AMCEs(all.weighted.public, all.donors, "Public", "Donors")
plot_AMCEs_comparison("Comparing Public vs. Donors (March 2019)", comparison10)

# Republican public vs. Republican donors

republican.donor <- df_cleaned_donors %>% filter(partisanship_dum == 1)
republican.donor <- estimate_AMCEs("AMCEs, Republican (Donors, March 2019)", republican.donor, model)
comparison11 <- compare_AMCEs(republican.public, republican.donor, "Republican Public", "Republican Donors")
plot_AMCEs_comparison("Comparing Republican Public vs. Donors (March 2019)", comparison11)

# Democrat public vs. Democrat donors 

democrat.donor <- df_cleaned_donors %>% filter(partisanship_dum == 2)
democrat.donor <- estimate_AMCEs("AMCEs, Democrat (Donors, March 2019)", democrat.donor, model)
comparison12 <- compare_AMCEs(democrat.public, democrat.donor, "Democratic Public", "Democratic Donors")
plot_AMCEs_comparison("Comparing Democratic Public vs. Donors (March 2019)", comparison12)

